package com.ruoyi.framework.util;

import org.crazycake.shiro.IRedisManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 描述：
 *
 * @author 史彦磊
 * @Date 2019-12-25 21:16
 */
@Component
public class RedisCacheUtil {


    @Autowired
    IRedisManager redisManager;

    /**
     * 获取缓存值
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(String key,Class<T> clazz){
        byte[] bytes = this.redisManager.get(key.getBytes(StandardCharsets.UTF_8));
        if (bytes != null){
            return JsonUtils.str2Obj(new String(bytes,StandardCharsets.UTF_8),clazz);
        }
        return null;
    }


    /**
     * 设置缓存值
     * @param key
     * @param obj
     * @param seconds
     */
    public void set(String key,Object obj,int seconds){
        this.redisManager.set(key.getBytes(StandardCharsets.UTF_8), JsonUtils.obj2Str(obj).getBytes(StandardCharsets.UTF_8), seconds);
    }


    /**
     * 清除缓存值
     * @param key
     */
    public void del(String key){
        this.redisManager.del(key.getBytes(StandardCharsets.UTF_8));
    }

}
